﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Galaxy
{
    
    public class GalaxyCommonDataFactory
    {
        public static GalaxyCommonData Create(int type)
        {
            return new GalaxyCommonData();
        }
    }
	
    //Item, Skill, Buff, ...Base Class
    public class GalaxyCommonData : GalaxyParamPoolOnwer
    {
        public int Index { get; set; }
        public void UpdateCommonData(byte[] buffer)
        {
            UpdataFromPacketBuffer(buffer);
        }
    }
   
    public class GalaxyCommonDataCont
    {
        public Dictionary<int, GalaxyCommonData> CommonDataMap;
        public Dictionary<int, GalaxyCommonData> CommonDataMapDataID;
        public int ParamType { get; set; }
        public delegate void CommonDataCallBack(GalaxyCommonData obj);
        public delegate void CommonDataRemoveCallback(int type);
        public CommonDataCallBack OnCreate { get; set; }
        public CommonDataCallBack OnDelete { get; set; }
        public CommonDataCallBack OnInit { get; set; }
        public CommonDataCallBack OnPreUpdate { get; set; }
        public CommonDataCallBack OnUpdate { get; set; }

        public CommonDataRemoveCallback OnClear { get; set; }
        public GalaxyCommonDataCont()
        {
            CommonDataMap = new Dictionary<int, GalaxyCommonData>();
            CommonDataMapDataID = new Dictionary<int, GalaxyCommonData>();
        }
        public void ClearCommonData()
        {
            CommonDataMap.Clear();
            CommonDataMapDataID.Clear();
            if(OnClear != null)
            {
                OnClear(ParamType);
            }
            OnCreate = null;
            OnDelete = null;
            OnInit = null;
            OnPreUpdate = null;
            OnUpdate = null;
        }
        public void AddCommonData(GalaxyCommonData data)
        {
            CommonDataMap[data.Index] = data;  
            CommonDataMapDataID[data.GetParamDataID()] = data;
        }
        public void RemoveCommonData(GalaxyCommonData data)
        {
            CommonDataMap[data.Index] = null;
            CommonDataMap.Remove(data.Index);
            CommonDataMapDataID[data.GetParamDataID()] = null;
            CommonDataMapDataID.Remove(data.GetParamDataID());

        }
        public GalaxyCommonData GetCommonData(int idx)
        {
            if (!CommonDataMap.ContainsKey(idx))
            {
                return null;
            }
            return CommonDataMap[idx];
        }

        public int GetCommonDataCount()
        {
            return CommonDataMap.Count;
        }

        public int[] GetAllDataID()
        {
            return CommonDataMapDataID.Keys.ToArray();
        }
        public int[] GetAllIndex()
        {
            return CommonDataMap.Keys.ToArray();
        }
        public GalaxyCommonData GetCommonDataByDataID(int dataID)
        {
            if (!CommonDataMapDataID.ContainsKey(dataID))
            {
                return null;
            }
            return CommonDataMapDataID[dataID];
        }
		//paramtype 高16位是类型，低16位是DataID
        public void InitCommonData(int idx, int paramType, byte[] buffer)
        {
            GalaxyCommonData data = GetCommonData(idx);
            if (data != null)
            {
                return;
            }
            data = GalaxyCommonDataFactory.Create(ParamType);
            ParamPool pool = ParamPoolDefine.CreateParamPool(paramType);
            data.SetParamPool(pool);
            data.Index = idx;
            AddCommonData(data);
            data.UpdateCommonData(buffer);
            if(OnInit != null)
            {
                OnInit(data);
            }
        }
        public void CreateCommonData(int idx, int paramType, byte[] buffer)
        {
            GalaxyCommonData data = GetCommonData(idx);
            if (data != null)
            {
                return;
            }
            data = GalaxyCommonDataFactory.Create(ParamType);
            ParamPool pool = ParamPoolDefine.CreateParamPool(paramType);
            data.SetParamPool(pool);
            data.Index = idx;
            AddCommonData(data);
            data.UpdateCommonData(buffer);
            if (OnCreate != null)
            {
                OnCreate(data);
            }
        }
        public void UpdateCommonData(int idx, byte[] buffer)
        {
            GalaxyCommonData data = GetCommonData(idx);
            if (data == null)
            {
                return;
            }
            if(OnPreUpdate != null)
            {
                OnPreUpdate(data);
            }
            data.UpdateCommonData(buffer);
            if (OnUpdate != null)
            {
                OnUpdate(data);
            }
        }
        public void RemoveCommonData(int idx)
        {
            GalaxyCommonData data = GetCommonData(idx);
            if (data == null)
            {
                return;
            }
            if (OnDelete != null)
            {
                OnDelete(data);
            }
            RemoveCommonData(data);           
        }
    }
    public class GalaxyCommonDataManager
    {
        public Dictionary<int, GalaxyCommonDataCont> CommonDataCont;
        public GalaxyCommonDataManager()
        {
            CommonDataCont = new Dictionary<int, GalaxyCommonDataCont>();
        }

        public void ClearAllData()
        {
            foreach (var item in CommonDataCont)
            {
                if (item.Value != null)
                {
                    item.Value.ClearCommonData();
                }
            }
        }
        public GalaxyCommonDataCont GetCont(int type, bool bCreate = true)
        {
            if (!CommonDataCont.ContainsKey(type))
            {
                if(bCreate)
                {
                    GalaxyCommonDataCont cont = new GalaxyCommonDataCont();
                    cont.ParamType = type;
                    CommonDataCont[type] = cont;
                    return cont;
                }
                return null;
            }
            return CommonDataCont[type];
        }
       
        public GalaxyCommonData GetCommonData(int type, int idx)
        {
            GalaxyCommonDataCont cont = GetCont(type);
            if (cont == null)
                return null;
            return cont.GetCommonData(idx);
        }

        public int GetCommonDataCount(int type)
        {
            GalaxyCommonDataCont cont = GetCont(type);
            if (cont == null)
                return 0;
            return cont.GetCommonDataCount();
        }
        public GalaxyCommonData GetCommonDataByDataID(int type, int dataID)
        {
            GalaxyCommonDataCont cont = GetCont(type);
            if (cont == null)
                return null;
            return cont.GetCommonDataByDataID(dataID);
        }
        public void InitCommonData(int idx, int paramType, byte[] buffer)
        {
			int paramId = ParamPool.GetParamType(paramType);
            GalaxyCommonDataCont cont = GetCont(paramId);
            if(cont == null)
            {
                return;
            }
            cont.InitCommonData(idx, paramType, buffer);
        }
        public void CreateCommonData(int idx, int paramType, byte[] buffer)
        {
			int paramId = ParamPool.GetParamType(paramType);
            GalaxyCommonDataCont cont = GetCont(paramId);
            if (cont == null)
            {
                return;
            }
            cont.CreateCommonData(idx, paramType, buffer);
        }
        public void UpdateCommonData(int idx, int paramType, byte[] buffer)
        {
            int paramId = ParamPool.GetParamType(paramType);
            GalaxyCommonDataCont cont = GetCont(paramId);
            if (cont == null)
            {
                return;
            }
            cont.UpdateCommonData(idx, buffer);
        }
        public void RemoveCommonData(int idx, int paramType)
        {
            int paramId = ParamPool.GetParamType(paramType);
            GalaxyCommonDataCont cont = GetCont(paramId);
            if (cont == null)
            {
                return;
            }
            cont.RemoveCommonData(idx);
        }
    }
}
